package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.OverseasSearchBean;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 境外人员查询
 */
public class OverseasSearchActivity extends BaseActivity implements IPublicBeanView{

    @BindView(R.id.et_Name)
    EditText et_Name;
    @BindView(R.id.et_ChainName)
    EditText et_ChainName;
    @BindView(R.id.et_NowAddress)
    EditText et_NowAddress;
    @BindView(R.id.sp_Sex) Spinner sp_Sex;
    @BindView(R.id.sp_Nationality) Spinner sp_Nationality;
    @BindView(R.id.sp_Profession) Spinner sp_Profession;
    @BindView(R.id.sp_Goal) Spinner sp_Goal;
    @BindView(R.id.tv_BirthStartTime) TextView tv_BirthStartTime;
    @BindView(R.id.tv_BirthEndTime) TextView tv_BirthEndTime;
    @BindView(R.id.tv_CredStartTime) TextView tv_CredStartTime;
    @BindView(R.id.tv_CredEndTime) TextView tv_CredEndTime;
    @BindView(R.id.tv_ComeStartTime) TextView tv_ComeStartTime;
    @BindView(R.id.tv_ComeEndTime) TextView tv_ComeEndTime;
    @BindView(R.id.tv_BackStartTime) TextView tv_BackStartTime;
    @BindView(R.id.tv_BackEndTime) TextView tv_BackEndTime;

    private List<GlobalBean> sexList,nationalityList,professionList,goalList;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_overseas_search);
        setTitleRight(R.string.search);
        setTitleName("境外人员查询");
        ButterKnife.bind(this);
        initData();
    }


    @OnClick({R.id.tv_right,R.id.tv_BirthStartTime,R.id.tv_BirthEndTime,R.id.tv_CredStartTime,
            R.id.tv_CredEndTime, R.id.tv_ComeStartTime,R.id.tv_ComeEndTime,R.id.tv_BackStartTime,
            R.id.tv_BackEndTime})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                submit();
                break;
            case R.id.tv_BirthStartTime:
                BaseUtil.createDataPop(this,tv_BirthStartTime);
                break;
            case R.id.tv_BirthEndTime:
                BaseUtil.createDataPop(this,tv_BirthEndTime);
                break;
            case R.id.tv_CredStartTime:
                BaseUtil.createDataPop(this,tv_CredStartTime);
                break;
            case R.id.tv_CredEndTime:
                BaseUtil.createDataPop(this,tv_CredEndTime);
                break;
            case R.id.tv_ComeStartTime:
                BaseUtil.createDataPop(this,tv_ComeStartTime);
                break;
            case R.id.tv_ComeEndTime:
                BaseUtil.createDataPop(this,tv_ComeEndTime);
                break;
            case R.id.tv_BackStartTime:
                BaseUtil.createDataPop(this,tv_BackStartTime);
                break;
            case R.id.tv_BackEndTime:
                BaseUtil.createDataPop(this,tv_BackEndTime);
                break;
        }
    }

    private void submit() {
        OverseasSearchBean searchBean=new OverseasSearchBean();
        searchBean.setEnglishName(et_Name.getText().toString().trim());
        searchBean.setName(et_ChainName.getText().toString().trim());
        searchBean.setCurrentAddress(et_NowAddress.getText().toString().trim());
        GlobalBean sBean= (GlobalBean) sp_Sex.getSelectedItem();
        String sexId="";
        if (sBean!=null){
            sexId=(sBean.getId()).equals("-1") ?"":sBean.getId();
        }
        searchBean.setGender(sexId);
        String nationalityId="";
        GlobalBean nBean= (GlobalBean) sp_Nationality.getSelectedItem();
        if (nBean!=null){
            nationalityId=(nBean.getId()).equals("-1") ?"":nBean.getId();
        }
        searchBean.setNationality(nationalityId);
        String profession="";
        GlobalBean pBean= (GlobalBean) sp_Profession.getSelectedItem();
        if (pBean!=null){
            profession=(pBean.getId()).equals("-1") ?"":pBean.getId();
        }
        searchBean.setProfessionId(profession);
        String goalId="";
        GlobalBean gBean= (GlobalBean) sp_Goal.getSelectedItem();
        if (gBean!=null){
            goalId=(gBean.getId()).equals("-1") ?"":gBean.getId();
        }
        searchBean.setInflowReason(goalId);
        MyRxBus.get().post(new MsgEvent<>("OverseasActivity",1,searchBean));
        finish();
    }

    private void initData() {
        PublicListPest publicListPest=new PublicListPest(this);
        publicListPest.getPublicList("性别");
        publicListPest.getPublicList("国籍");
        publicListPest.getPublicList("来华目的");
        publicListPest.getPublicList("行业性质");

    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("性别".equals(publicBean.getDomainName())){
            sexList=publicBean.getDicts();
            sexList.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Sex,sexList);
        }
        if ("国籍".equals(publicBean.getDomainName())){
            nationalityList=publicBean.getDicts();
            nationalityList.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Nationality,nationalityList);
        }
        if ("行业性质".equals(publicBean.getDomainName())){
            professionList=publicBean.getDicts();
            professionList.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Profession,professionList);
        }
        if ("来华目的".equals(publicBean.getDomainName())){
            goalList=publicBean.getDicts();
            goalList.add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Goal,goalList);
        }

    }

    private void setSpinnerAdapter(Spinner spinner, List<GlobalBean> lists) {
        SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,lists);
        spinner.setAdapter(adapter);
    }
}
